﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace AutomaticAdvertising
{
    public delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e);

    public class Scheduler
    {
        private bool m_IsStarted = false;
        private Timer m_Timer = new Timer();

        public event ElapsedEventHandler SchedulerTask;
        
        public Scheduler()
        {
        }

        public void Start(int i_UpdateInterval)
        {
            if (!m_IsStarted)
            {
                m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
                m_Timer.Interval = i_UpdateInterval;
                m_Timer.Enabled = true;

                m_IsStarted = true;
            }
            else if (!m_Timer.Enabled)
            {
                Resume();
            }
        }

        public void Pause()
        {
            m_Timer.Enabled = false;
        }

        public void Resume()
        {
            m_Timer.Enabled = true;
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            if (SchedulerTask != null)
            {
                SchedulerTask(source, e);
            }
        }
    }
}
